package com.xiaodu.validator.config;

import org.springframework.web.multipart.MultipartFile;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author: dutianjie
 * @date: 2021/3/31
 * @version: v1.0.0
 * @description: 描述
 **/

public class ValidatedFileSufixx implements ConstraintValidator<FileSufixx, MultipartFile> {

    private String fileSufixx;

    @Override
    public boolean isValid(MultipartFile value, ConstraintValidatorContext context) {
        if (fileSufixx == null || fileSufixx.trim().equals("")) return true;

        String originalFilename = value.getOriginalFilename();
        if (originalFilename.contains(".")) {
            if (originalFilename.substring(originalFilename.lastIndexOf(".") + 1).equals(fileSufixx)) {
                return true;
            }
        }

        return false;
    }

    @Override
    public void initialize(FileSufixx constraintAnnotation) {
        fileSufixx = constraintAnnotation.value();
    }
}
